package sos

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"xinxiang-mutual-platform/model/common"
	"xinxiang-mutual-platform/model/common/request"
	"xinxiang-mutual-platform/model/common/response"
	"xinxiang-mutual-platform/model/sos"
)

type SOSApi struct{}

// 调用服务层的sevice对象：SOSService
func (s SOSApi) CreateSOS(c *gin.Context) {
	//实例化
	var api sos.Sos
	//1. 解析参数
	if err := c.ShouldBindJSON(&api); err != nil {
		response.FailWithDitals(common.ErrorInvalidParams, c)
		return
	}
	fmt.Println(api)
	//2.业务处理
	err := SOSService.CreateSOS(api)
	//3. 返回响应
	if err != nil {
		response.FailWithDitals(common.ErrorInsert, c)
	} else {
		response.OKWithDitals(common.SUCCESS, nil, c)
	}
}
func (s SOSApi) GetSOSList(c *gin.Context) {
	// 获取条件
	req := request.Request{}
	err := c.ShouldBindQuery(&req)
	fmt.Println("请求参数:", req)
	if err != nil {
		fmt.Println("err", err)
		response.FailWithDitals(common.ErrorInvalidParams, c)
		return
	}

	// 调用服务层返回 结果
	list, total, err := SOSService.GetSOSInfoList(&req)
	if err != nil {
		fmt.Println(err)
		response.FailWithDitals(common.ErrorServerSeache, c)
		return
	}

	// 封装返回
	response.OKWithDitals(common.SUCCESS, response.PageResult{
		list,
		total,
		req.PageInfo.Page,
		req.PageInfo.PageSize,
	}, c)
}
